package handlers

import (
	"github.com/gin-gonic/gin"
	"login-pro/models"
	"login-pro/pkg"
	"strconv"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if password != pkg.Md5Str(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  " 密码加密",
		})
		return
	}
	data := models.User{
		Username: username,
		Password: password,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
		return
	}
	//userID :=
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	var count int64
	models.DB.Model(&models.Book{}).Count(&count)

	offset := (page - 1) * size
	var book []models.Book
	err := models.DB.Limit(size).Offset(offset).Find(&book)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data":gin.H{
			"book":book,
			"count":count,
		}.
	})
	return
}
